package com.zk.application.test.copy;

import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyFactory;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.spec.PKCS8EncodedKeySpec;

import javax.net.ssl.SSLContext;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;

import com.aliyun.configuration.ConfigurationTool;
import com.certificate.tool.SetTrustStore;
import com.common.constant.Constant;

/* 2016.9.2
 * 在阿里云上，创建一个zk应用,
 * 测试，
 * 1、compose yaml 字符串  中传入参数
 * 2、想想如何根据用户传入的集群个数，集群名称，集群描述
 */
public class CreateZookeeperApp {
	
	public static String main(String[] args) {
	String nodes=args[0];
	String clusterName=args[1];
	String clusterDesc=args[2];
		CreateZookeeperApp app=new CreateZookeeperApp();
		return app.CreateZookeeperCluster(nodes, clusterName, clusterDesc);
	}
	
	public String CreateZookeeperCluster(String nodes,String clusterName,String clusterDesc)
	{
		 //get certificate address
		String returnCode="400";
		 try {
			 SSLConnectionSocketFactory sslsf = SetTrustStore.getSSLConnection();
			 //httpclient链接
			 CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
			 
			 HttpPost httpPost = new HttpPost(ConfigurationTool.getProperties(Constant.ALIYUN_PROJECTS_URL));
			 
			 httpPost.addHeader("Content-type","application/json;charset=UTF-8");
			 
			 StringBuffer composeYaml = new StringBuffer();
			 //集群中，节点的个数； 需要用户传入
			 int nodeNum = Integer.valueOf(nodes); 
			 
			 for(int i = 1; i <= nodeNum; i++ ){
				 
				 composeYaml.append(clusterName + "-" + ConfigurationTool.getProperties(Constant.ZK_CONTAINER_NAME)+ i + ConfigurationTool.getProperties(Constant.ZK_CONTAINER_NAME_SUFFIX))
					.append("  " + ConfigurationTool.getProperties(Constant.ZK_IMAGE))
					.append("  " + ConfigurationTool.getProperties(Constant.ZK_HOSTNAME)+ i + ConfigurationTool.getProperties(Constant.NEWLIN_SYSBOL))
					.append("  " + ConfigurationTool.getProperties(Constant.ZK_VOLUMES) 
									+ "    " + ConfigurationTool.getProperties(Constant.ZK_VOLUMESLOCAL_PATH)+ clusterName + ConfigurationTool.getProperties(Constant.ZK_VOLUMESLOCAL_NAME) + ConfigurationTool.getProperties(Constant.ZK_VOLUMESLOCALNAME_CONF)
//									+ "    " + ConfigurationTool.getProperties(Constant.ZK_VOLUMESLOCAL_PATH) + ConfigurationTool.getProperties(Constant.ZK_VOLUMESLOCAL_NAME) + i + ConfigurationTool.getProperties(Constant.ZK_VOLUMES_SUFFIX_DATA)
//									+ "    " + ConfigurationTool.getProperties(Constant.ZK_VOLUMESLOCAL_PATH) + ConfigurationTool.getProperties(Constant.ZK_VOLUMESLOCAL_NAME) + i + ConfigurationTool.getProperties(Constant.ZK_VOLUMES_SUFFIX_LOGS)
									)
					.append("  " + ConfigurationTool.getProperties(Constant.ZK_RESTART))
					.append("  " + ConfigurationTool.getProperties(Constant.ZK_PORTS))
					.append("  " + ConfigurationTool.getProperties(Constant.ZK_ENVIRONMENT_A) + i + ConfigurationTool.getProperties(Constant.NEWLIN_SYSBOL)) 
					.append(ConfigurationTool.getProperties(Constant.ZK_OTHERARGS))
					;
			 }
			 
			 /*
			  * appName: 集群的名称，
			  * appDes:  集群的描述
			  */
			 
			 String appName = clusterName;
			 
			 String appDesc = clusterDesc;
			 
			 String  requestEntity =
					 "{\"name\":"+"\""+appName+"\","
					 
					+ "\"description\":"+"\""+appDesc+"\","
					
					+ "\"template\":"+"\""+composeYaml.toString()+"\""+","
					
					+ "\"version\":\"1.0\"}";
			 
			 StringEntity stringEntity = new StringEntity(requestEntity, Charset.forName("UTF-8"));
			 
			 httpPost.setEntity(stringEntity);

			 //使用客户端向服务器发送数据
			 HttpResponse httpResponse = httpClient.execute(httpPost);
			 
			 if (httpResponse.getStatusLine().getStatusCode() == 201) {
				 returnCode="201";
				System.out.println(httpResponse.getProtocolVersion()
						+ " " + httpResponse.getStatusLine().getStatusCode()
						+ " " + httpResponse.getStatusLine().getReasonPhrase());
				
				System.out.println("OK:201");
			} else {
				HttpEntity resultEntity = httpResponse.getEntity();
				
				//返回结果是，服务器上，返回来的是json字符串数据
				String strResultEntity = EntityUtils.toString(resultEntity);
				
				System.out.println(strResultEntity);
				
				System.out.println(httpResponse.getStatusLine().getStatusCode());
				
				System.out.println("Something is wrong!!!");
				 returnCode="400";
			}
		
			} catch (Exception e) {
				 returnCode="400";
				e.printStackTrace();
			}
		 return  returnCode;
	}
}
		
		
		
